//
//  WebViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/3.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //載入設定要讀取的網址
    [self loadRequestFromString:self.webURLString];
    
    //設定進度條
    [self progress:0 withMessage:nil];

    //隱藏tabBarController的TabBar
    self.tabBarController.tabBar.hidden = YES;

}
-(void)viewWillDisappear:(BOOL)animated
{
    [self.pv removeFromSuperview];
    self.tabBarController.tabBar.hidden = NO;

}
- (void)loadRequestFromString:(NSString*)urlString
{
    //    NSLog(@"%@",urlString);
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];

}

- (void)progress:(int)progress withMessage:(NSString*)message {
    
    float height = 1;
    float y = self.navigationController.navigationBar.frame.size.height - height;
    
    self.pv = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar] ;
    //    NSLog(@"%f",self.navigationItem.leftBarButtonItem.width);
    self.pv.frame = CGRectMake(0, y, 320, height);
    self.pv.progress = progress/100.0;
    [self.navigationController.navigationBar addSubview:self.pv];
    [self.pv setHidden:YES];
}
- (void)updateButtons
{
    self.forward.enabled = self.webView.canGoForward;
    self.back.enabled = self.webView.canGoBack;
    self.stop.enabled = self.webView.loading;
    //        self.forward.enabled = self.webView.canGoForward;
    //        self.back.enabled = self.webView.canGoBack;
    //        self.stop.enabled = self.webView.loading;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.pv setHidden:NO];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self updateButtons];
    [self.pv setProgress:0.8 animated:YES];
    //    NSLog(@"開始%f",pv.progress);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self updateButtons];
    [self.pv setProgress:1.0 animated:YES];
    //    NSLog(@"完成%f",pv.progress);
    
    [self performSelector:@selector(hiddenPV) withObject:nil afterDelay:0.5f];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self updateButtons];
    [self.pv setProgress:1.0 animated:YES];
    //    NSLog(@"失敗%f",pv.progress);
    
    [self performSelector:@selector(hiddenPV) withObject:nil afterDelay:0.5f];
    
}
-(void)hiddenPV
{
    [self.pv setProgress:0];
    [self.pv setHidden:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
